package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.model.Admin;
import com.xy.model.DeliverToStation;
import com.xy.model.DeliverToStudent;
import com.xy.model.DeliverToTeacher;
import com.xy.model.TextBook;
import com.xy.service.DeliverToStationService;
import com.xy.service.DeliverToTeacherService;
import com.xy.service.impl.DeliverToStationServiceImpl;
import com.xy.service.impl.DeliverToStudentServiceImpl;
import com.xy.service.impl.DeliverToTeacherServiceImpl;
import com.xy.service.impl.StationServiceImpl;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class DeliverToStationServlet
 */
@WebServlet("/deliverToStationServlet")
public class DeliverToStationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeliverToStationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DeliverToStationService service = new DeliverToStationServiceImpl();
			
		if("add".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));//textbookId
			int staId = Integer.valueOf(request.getParameter("station"));
			TextBook tb = new TextBookServiceImpl().get(id);
			DeliverToStation dts = new DeliverToStation();
			dts.setAdmin((Admin) request.getSession().getAttribute("admin"));
			dts.setAmount(Integer.valueOf(request.getParameter("amount")));
			dts.setCreateDate(new Date());
			dts.setDeliverDate(new Date());
			dts.setStation(new StationServiceImpl().get(staId));
			dts.setTextBook(tb);
			service.add(dts);
			request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
		}else  if("list".equals(request.getParameter("status"))) {
			request.setAttribute("dtss", service.list(DeliverToStation.class));
			request.getRequestDispatcher("/pages/storage/storage001/station/deliver_query.jsp").forward(request, response);
		}else if("query".equals(request.getParameter("status"))) {
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			String hql = "from DeliverToStation where deliverTime>='"+start+"'"
					+" and deliverTime<='"+end+"'";
			List<DeliverToStation> dtss = new DeliverToStationServiceImpl().queryByHql(hql);
			request.setAttribute("dtss", dtss);
			request.getRequestDispatcher("/pages/storage/storage001/station/deliver_query.jsp").forward(request, response);
		}
	}

}
